package net.alche.al.util.alches;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.List;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.stream.StreamUtil;
import net.alche.al.util.AC;

/**
 * ToolFile
 */
public class ToolFile {

  Charset utf8 = null;

  public Charset getUtf8() {
    if (utf8 == null)
      utf8 = Charset.forName("UTF-8");
    return utf8;
  }

  public String readString(String filePath) {
    return FileUtil.readString(filePath, getUtf8());
  }

  public String readClassRes(String path) {
    InputStream ips = AC.system.getClassPathRes(path);
    try {
      byte[] byts = new byte[ips.available()];
      ips.read(byts);
      String str = new String(byts, getUtf8());
      return str;
    } catch (IOException e) {
      e.printStackTrace();
      return "";
    }
  }

  public List<String> readLinesClassRes(String path) {
    URL url = ToolFile.class.getResource(path);
    return FileUtil.readUtf8Lines(url);
  }

  /**
   * 文件扩展名称
   * 
   * @param fileName 文件名称
   * @return
   */
  public String extName(String fileName) {
    return FileUtil.extName(fileName);
  }

  public File[] ls(String path) {
    if (!FileUtil.exist(path)) {
      return new File[] {};
    }
    return FileUtil.ls(path);
  }
}